package com.manage.service;

import java.util.List;

import org.apache.log4j.Logger;

import com.manage.config.Config;
import com.manage.model.SpCodeFilter;
//import com.manage.model.vo.VSpCodeFilter;
import com.service.AbstractService;
import com.util.ConfigUtil;
import com.utils.dbcp.ConnectionManager;
import com.utils.dbcp.ModelGenerator;

public class SpCodeFilterServiceImpl extends AbstractService implements SpCodeFilterService{
	private Logger logger;
	public SpCodeFilterServiceImpl()
	{
		logger = Logger.getLogger(this.getClass());
		this.dbcp = ConfigUtil.get(Config.SECTION, Config.KEY_DBCP_NAME);
	}
	@Override
	public SpCodeFilter getSpCodeFilterById(int id) {
		String sql = "select * from spcodefilter where id="+id;
		ModelGenerator<SpCodeFilter> dbcpmg = new ModelGenerator<SpCodeFilter>(SpCodeFilter.class);
		return dbcpmg.generate(dbcp, sql);
	}

	@Override
	public List<SpCodeFilter> getSpCodeFiltersBySpCodeId(int spCodeId) {
		String sql = "select * from spcodefilter where spcodeid="+spCodeId+" order by createdate desc";
		ModelGenerator<SpCodeFilter> dbcpmg = new ModelGenerator<SpCodeFilter>(SpCodeFilter.class);
		return dbcpmg.generateList(dbcp, sql);
	}

	@Override
	public boolean saveSpCodeFilter(SpCodeFilter spCodeFilter, String type) {
		String sql ="";
		if(type.equalsIgnoreCase("insert"))
		{
			spCodeFilter.setId(getTableId("spcodefilter"));
			sql = "insert into spcodefilter(id,spcodeid,filtertype,filterport,filtercontent1,filtercontent2" +
					",filtercounter,filterDate,createuser,createdate,filterprovince) values (" +
					""+spCodeFilter.getId()+",'"+spCodeFilter.getSpCodeId()+
					"','"+spCodeFilter.getFiltertype()+
					"','"+spCodeFilter.getFilterPort()+"','"+spCodeFilter.getFilterContent1()+
					"','"+spCodeFilter.getFilterContent2()+
					"','"+spCodeFilter.getFilterCounter()+"','"+spCodeFilter.getFilterDate()+
					"','"+spCodeFilter.getCreateUser()+
					"','"+spCodeFilter.getCreateDate()+"','"+spCodeFilter.getFilterProvince()+"')";
		}else if (type.equalsIgnoreCase("update"))
		{
			sql="update spcodefilter set filtertype='"+spCodeFilter.getFiltertype()+
				"',filterport='"+spCodeFilter.getFilterPort()+
				"',filtercontent1='"+spCodeFilter.getFilterContent1()+
				"',filtercontent2='"+spCodeFilter.getFilterContent2()+
				"',filtercounter='"+spCodeFilter.getFilterCounter()+
				"',filterDate='"+spCodeFilter.getFilterDate()+
				"',filterprovince='"+spCodeFilter.getFilterProvince()+"' where " +
					"id ="+spCodeFilter.getId();
		}
		else if (type.equalsIgnoreCase("delete"))
		{
			sql="delete from spcodefilter where " +
			"id ="+spCodeFilter.getId();
		}
		return ConnectionManager.getInstance().execute(dbcp, sql);
	}
//	@Override
//	public List<VSpCodeFilter> getSpCodeFilters() {
//		String sql = "select * from spcodefilter  order by createdate desc";
//		ModelGenerator<VSpCodeFilter> dbcpmg = new ModelGenerator<VSpCodeFilter>(VSpCodeFilter.class);
//		return dbcpmg.generateList(dbcp, sql);
//	}

}
